package com.zsr.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsr.example.entity.EduCourse;
import com.zsr.example.entity.EduSubject;
import com.zsr.example.entity.EduTeacher;
import com.zsr.example.mapper.IndexMapper;
import com.zsr.example.service.*;
import com.zsr.example.util.PageUtil;
import com.zsr.example.util.constants.CourseConstants;
import com.zsr.example.vo.SubjectNestedVo;
import org.apache.commons.collections4.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 自定义首页serviceImpl
 *
 * @author zsr
 * @date 2021/6/18 20:42
 */
@Service
public class IndexFrontServiceImpl implements IndexFrontService {
    @Autowired
    private EduCourseService courseService;
    @Autowired
    private EduTeacherService eduTeacherService;
    @Autowired
    private EduSubjectService eduSubjectService;
    @Autowired
    private RedisService redisService;
    @Autowired
    private IndexMapper indexMapper;

    //TODO 考虑清空redis
    @Cacheable(value = "eduCourse", key = "'selectIndexList'", cacheManager = "redisCacheManager")
    @Override
    public List<EduCourse> getHotCourseList() {
        QueryWrapper<EduCourse> wrapper = new QueryWrapper<>();
        wrapper.select("id", "title", "price", "lesson_num", "cover", "class_tag", "buy_count", "view_count");
        wrapper.eq("status", CourseConstants.STATUS_NORMAL);
        wrapper.orderByDesc("id");
        wrapper.last("limit 8");
        List<EduCourse> list = courseService.list(wrapper);
        /*Random r = new Random(1);
        String[] arr={"","up","new"};
        list.forEach(item->{
            item.setClassTag(arr[r.nextInt(3)]);
        });*/
        return list;
    }

    @Cacheable(value = "eduTeacher", key = "'selectIndexList'", cacheManager = "redisCacheManager")
    @Override
    public List<EduTeacher> getFourTeacherList() {
        QueryWrapper<EduTeacher> wrapperTeacher = new QueryWrapper<>();
        wrapperTeacher.select("id", "name", "intro", "career", "avatar");
        wrapperTeacher.orderByDesc("id");
        wrapperTeacher.last("limit 4");
        List<EduTeacher> teacherList = eduTeacherService.list(wrapperTeacher);
        return teacherList;
    }

    @Cacheable(value = "eduSubject", key = "'selectIndexList'", cacheManager = "redisCacheManager")
    @Override
    public List<SubjectNestedVo> getSubjectNestedList(Long count) {
        List<SubjectNestedVo> subjectList = eduSubjectService.getAllSubject(count);
        return subjectList;
    }

    @Override
    public Map<String, Object> searchByKeyWord(Page<EduCourse> pageParam, String keyword) {

        List<EduCourse> eduCourses = courseService.searchCourse(keyword);
        long current = pageParam.getCurrent();
        long size = pageParam.getSize();
        long total = eduCourses.size();
        long pages = (total + size - 1) / size;

        boolean hasNext = current + 1 <= pages;
        boolean hasPrevious = current - 1 >= 1;

        List<EduCourse> records = PageUtil.build((int) current, (int) size, eduCourses);

        QueryWrapper<EduSubject> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", keyword);
        List<String> searchList = eduSubjectService.list(queryWrapper).stream().map(EduSubject::getTitle).collect(Collectors.toList());

        if (!CollectionUtils.isEmpty(records)) {
            for (EduCourse record : records) {
                redisService.searchZincrby(record.getTitle());//将所有课程名加入热搜榜
            }
        }
        if (!CollectionUtils.isEmpty(searchList)) {
            for (String item : searchList) {
                redisService.searchZincrby(item);//将所有分类加入热搜榜
            }
        }

        Map<String, Object> map = new HashMap<>();
        map.put("subTitles", searchList);//可能查询的关键字
        map.put("items", records);//去重
        map.put("current", current);
        map.put("pages", pages);
        map.put("size", size);
        map.put("total", total);
        map.put("hasNext", hasNext);
        map.put("hasPrevious", hasPrevious);
        return map;
    }

    @Override
    public int getMsgCount(String uId) {
        return indexMapper.getMsgCount(uId);
    }
    @Override
    public void doRead(String uId){
        indexMapper.doRead(uId);
    }
}
